package com.hutu.kboshi.dao;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hutu.kboshi.mapper.QuestionBankQuestionMapper;
import com.hutu.kboshi.pojo.entity.QuestionBankQuestion;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class QuestionBankQuestionDao extends ServiceImpl<QuestionBankQuestionMapper, QuestionBankQuestion> implements IService<QuestionBankQuestion> {

    /**
     * NOTE LambdaQueryWrapper 默认还是会返回完整的实体对象，即使你只选择了部分字段。
     * 这是因为 MyBatis Plus 在底层构建 SQL 查询时，它会使用你指定的选择条件来优化 SQL 语句，
     * 但最终的结果集仍然会被映射到实体类上
     * @param questionIdList
     * @return
     */
    public List<Long> batchIdsByQuestionIds(List<Long> questionIdList) {
        return lambdaQuery().select(QuestionBankQuestion::getId)
                .in(QuestionBankQuestion::getQuestionId, questionIdList)
                .list().stream() // 转换为流
                .map(QuestionBankQuestion::getId) // 映射到 id
                .collect(Collectors.toList()); // 收集为 List<Long>
    }

    public void batchDeleteQuestionBankQuestionByIds(List<Long> questionBankQuestionIds) {
        if (CollectionUtil.isNotEmpty(questionBankQuestionIds)){
            baseMapper.deleteBatchIds(questionBankQuestionIds);
        }
    }

    public void removeQuestionBankQuestionByBankId(long id) {
        baseMapper.delete(lambdaQuery().eq(QuestionBankQuestion::getQuestionBankId, id));
    }

    public Map<Long,Integer> queryQuestionBankQuestionMax(List<Long> questionBankId) {
        return baseMapper.queryQuestionBankQuestionMax(questionBankId);
    }

    public QuestionBankQuestion queryQuestionBankQuestion(Long questionBankId, long questionId) {
        return lambdaQuery().eq(QuestionBankQuestion::getQuestionBankId, questionBankId)
                .eq(QuestionBankQuestion::getQuestionId, questionId)
                .one();
    }

    public List<QuestionBankQuestion> queryQuesBankQuesByQuesId(long questionId) {
        return lambdaQuery().eq(QuestionBankQuestion::getQuestionId, questionId)
                .list();
    }

    public void deleteQuesBankQuesByQuesId(long questionId) {
        baseMapper.delete(lambdaQuery().eq(QuestionBankQuestion::getQuestionBankId, questionId));
    }

    public void addBatchQuestionBankQuestion(List<Long> questionBankId, long questionId) {
        Map<Long, Integer> maxNumbers = baseMapper.queryQuestionBankQuestionMaxList(questionBankId);
        List<QuestionBankQuestion> questionBankQuestionList = questionBankId.stream().map(id -> {
            QuestionBankQuestion questionBankQuestion = new QuestionBankQuestion();
            questionBankQuestion.setQuestionBankId(id);
            questionBankQuestion.setQuestionId(questionId);
            questionBankQuestion.setQuestionOrder(maxNumbers.getOrDefault(id, 0) + 1);
            questionBankQuestion.setCreateBy(StpUtil.getLoginIdAsLong());
            return questionBankQuestion;
        }).collect(Collectors.toList());
        baseMapper.saveBatch(questionBankQuestionList);
    }

    public Page<QuestionBankQuestion> findQuestionBankIdsByQuestionIds(Page<QuestionBankQuestion> page, List<Long> questionIds) {
        return lambdaQuery().in(QuestionBankQuestion::getQuestionId, questionIds).page(page);
    }
}